package com.lw.extraceim.ui.fragments;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.lw.extraceim.R;
import com.lw.extraceim.app.ExtraceApplication;
import com.lw.extraceim.loader.PackageLoader;
import com.lw.extraceim.model.TransPackage;
import com.lw.extraceim.net.HttpResponseParam;
import com.lw.extraceim.net.ResponseCode;
import com.lw.extraceim.net.callback.HttpResponseParamCallback;
import com.lw.extraceim.ui.activity.tp.TransportInfoActivity;
import com.lw.extraceim.ui.adapter.TransPackageAdapter;
import com.lw.extraceim.ui.fragments.base.BaseFragment;
import com.lw.extraceim.util.JsonUtils;

import org.json.JSONException;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

public class PackageListFragment extends BaseFragment {
    private static final String TAG = "PackageListFragment";

    private ListView listView;
    private List<TransPackage> transPackageList;//目前的假数据
    private PackageLoader loader;

    public PackageListFragment(Context mContext, ExtraceApplication application) {
        super(mContext, application);
    }

    @Override
    public View initView() {
        View view = View.inflate(mContext, R.layout.layout_package_list, null);
        listView = view.findViewById(R.id.listview_package_list);
        return view;
    }

    @Override
    public void initData() {
        loader = new PackageLoader(getContext());
        //造点假数据
        loadPackageList();
    }

    public void initEvent() {
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                TransPackage transPackage = datas.get(position);
//                if (transPackage != null) {
//                    TransportInfoActivity.actionStart(getActivity(), transPackage);
//                }
            }
        });
    }

    @Override
    public void refreshUI() {

    }


    public void loadPackageList(){

        loader.getPackageListByUserId(application.getUserInfo().getuId(), 0, 0, new HttpResponseParamCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                alert("网络错误" + e.getMessage());
            }

            @Override
            public void onResponse(HttpResponseParam response, int id) {
                Log.d(TAG, "onResponse: " + response.reseponseJson);
                if(response.code.getCode() == ResponseCode.SUCESS){
                    try {
                        transPackageList = JsonUtils.fromJson(response.reseponseJson.getJSONArray("packageList").toString(),new TypeToken<List<TransPackage>>(){});
                        refreshPackageList();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }else{
                    alert(response.code.getMessage());
                }
            }
        });
    }

    /**
     * 刷新包裹列表
     */
    public void refreshPackageList(){

    }
}
